from django.db import models
from apps.users.models import User


class Course(models.Model):
    STATUS_CHOICES = [
        ('active', '开放选课'),
        ('inactive', '暂停'),
        ('ended', '已结束'),
    ]

    course_code = models.CharField(max_length=20, unique=True, verbose_name='课程编号')
    course_name = models.CharField(max_length=100, verbose_name='课程名称')
    description = models.TextField(blank=True, null=True, verbose_name='课程描述')
    teacher = models.ForeignKey(User, on_delete=models.RESTRICT, verbose_name='授课教师')
    credit_hours = models.IntegerField(default=2, verbose_name='学分')
    class_hours = models.IntegerField(default=32, verbose_name='学时')
    capacity = models.IntegerField(default=50, verbose_name='课程容量')
    enrolled_count = models.IntegerField(default=0, verbose_name='已选课人数')
    course_image = models.CharField(max_length=255, blank=True, null=True, verbose_name='课程图片路径')
    semester = models.CharField(max_length=20, verbose_name='学期')
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='active', verbose_name='状态')
    start_date = models.DateField(blank=True, null=True, verbose_name='开课日期')
    end_date = models.DateField(blank=True, null=True, verbose_name='结课日期')
    schedule = models.CharField(max_length=255, blank=True, null=True, verbose_name='上课时间安排')
    classroom = models.CharField(max_length=50, blank=True, null=True, verbose_name='上课教室')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'courses'
        verbose_name = '课程'
        verbose_name_plural = '课程'

    def __str__(self):
        return f"{self.course_code} - {self.course_name}"